class Solution {
public:
    bool check(vector<int>& bloomDay,int m,int k,int mid){
        int ret = 0;
        int n = bloomDay.size();
        int count = 0;
        for(int i = 0;i < n;i++){
            if(bloomDay[i] <= mid){
                count++;
            }else{
                count = 0;
            }
            if(count == k){
                ret++;
                count = 0;
            }
        }
        return ret >= m;
    }                

    int minDays(vector<int>& bloomDay, int m, int k) {
        int n = bloomDay.size();
        if(n / m < k) return -1;
        int Min = INT_MAX;
        int Max = 0;
        for(int i = 0;i < n;i++){
            Min = min(Min,bloomDay[i]);
            Max = max(Max,bloomDay[i]);
        }
        
        int l = Min;
        int r = Max;
        while(l <= r){
            int mid = l + (r - l) / 2;
            if(check(bloomDay,m,k,mid)){
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        return l;
    }
};
